<?php
/**
 * Created by PhpStorm.
 * User: toshiba
 * Date: 2015/4/17
 * Time: 18:23
 */

class user extends Activity{
    /** @var  UserModule  */
    protected $userModule;
    function __construct(){
        $this->userModule=UserModule::init('user');
    }

    function getUserInfoTask(){
        if(!$this->userModule->checkLogin()){
            $r['state']=DataMessage::STATE_WARRING;
            $r['message']="未登录";
            $r['user']=null;
            $r['userInfo']=null;
        }else{
            $r['state']=DataMessage::STATE_SUCCESS;
            $r['message']="success";
            $r['user']=$this->userModule->getLoginInfo();
            $r['userInfo']=UserMode::init()->getDetail($this->userModule->getLoginId());
        }
        View::displayAsJson($r);
    }

    function getCouponPoint($user_id){
        $user_id=(int)$user_id;
        $r['data']['coupon']=CouponMode::init()->getListByUser($user_id,null,true);
        $r['data']['point']=PointMode::init()->getPoint($user_id);
        return $r;
    }
    //【当前】用户支付券，积分
    function getCouponPointTask(){
        $user_id=$this->userModule->getLoginId();
        $r=$this->getCouponPoint($user_id);
        $r['resultCode']= !empty($user_id) ? 200:500;
        View::displayAsJson($r);
    }

    //添加收藏
    function addToCollectTask($user_id,$gopen_id){
        //$r=CollectModule::init('user_collect','user_id','gopen_id')->add($user_id,$gopen_id);
        $c=CollectModule::init('user_collect','user_id','gopen_id');
        $checkExist=$c->checkExist($user_id,$gopen_id);
        if($checkExist){
            $r['resultMsg']="您已经收藏过了";
            $r['resultCode']= 404;
        }else{
            $r['data']=$c->add($user_id,$gopen_id);
            //这里不知道什么原因
            $r['resultCode']= $r['data']!=-1 ? 200:500;
        }
        View::displayAsJson($r);
    }
    //取消收藏
    function removeFromCollectTask($id){
        $result=CollectModule::init('user_collect','user_id','gopen_id')->remove($id);
        $r['resultCode']= $result ? 200:500;
        View::displayAsJson($r);
    }
    //取消收藏
    function deleteBrowseTask($id){
        $r=UserMode::init()->deleteBrowse($id);
        $result['resultCode']= $r ? 200:500;
        View::displayAsJson($result);
    }
    /**
     * 发验证码接口!
     * @param $tel
     * @param $type register|reset|pay|agent
     */
    function getCodeTask($tel,$type = 'register',$amount=null){
        switch($type){
            case 'register': break;
            case 'reset': break;
            case 'pay': break;
            case 'agent': break;
            default:$type='register';
        }
        $juhe=JuheSMSMode::init();
        $code=$this->userModule->getCode($tel);
        //if(!empty($amount)){
        if($type=='pay'){
            $juheRe=$juhe->send($tel, $type, array("#code#"=>$code,"#amount#"=>$amount));
        }else{
            $juheRe=$juhe->send($tel, $type, array("#code#"=>$code));
        }
        $re=new DataMessage($juheRe['error_code']==0? DataMessage::STATE_SUCCESS: DataMessage::STATE_ERROR, $juheRe['reason']);
        View::displayDataMessage($re);
    }
    function checkCodeTask($tel, $code){
        $userMode=UserModule::init("user");
        $re= $userMode->checkCode($tel, $code);
        View::displayDataMessage($re);
    }
    function checkCodeAndGetInfoTask($tel,$code){
        $r=$this->userModule->checkCode($tel,$code);
        //test
        $re = $this->getCouponPoint(1);
        //test
        if($r->state==DataMessage::STATE_SUCCESS){
            $user_id=UserMode::init()->getIdByUsername($tel);
            $re=$this->getCouponPoint($user_id);
            $re['resultCode']= !empty($re['data']) ? 200:500;
        }else{
            $re['resultCode']=404;
        }
        View::displayAsJson($re);
    }
} 